// void, Obj me, point pt

Unit this;
Obj u;

Sleep(rand(100)+30);
this = me.AsUnit();

if (.IsValid) {// leave parry mode at new command
	.SetParryMode(false);
	
	if(.hero.IsValid){
		if(.hero.HasItem("irage_battle")){
			Sleep(rand(200)+50);
			.AddCommand(true, "engage");
			return;
		} else if(.hero.HasItem("irage_battle_stand")){
			Sleep(rand(200)+50);
			.AddCommand(true, "stand_position");
			return;
		}
	}
	
	if(.item_count > 0){
		Item item;
		
		item = .FindItem("Terrain bonus I");
		if(item.IsValid)
			.RemoveItemsOfType("Terrain bonus I");
		else {
			item = .FindItem("Terrain bonus II");
			if(item.IsValid)
				.RemoveItemsOfType("Terrain bonus II");
			else {
				item = .FindItem("Terrain bonus III");
				if(item.IsValid)
					.RemoveItemsOfType("Terrain bonus III");
				else {
					item = .FindItem("Terrain bonus IV");
					if(item.IsValid)
						.RemoveItemsOfType("Terrain bonus IV");
				}
			}
		}
	}
}
 
while(1){
	//pr("advance: searching for new enemy");
	u = .BestTargetInSquadSight();
	
	if(u.IsAlive())
	if(.IsValidTarget(u)){
    if(.DistTo(u.pos) > .min_range)
		if(u.IsMilitary || u.IsHeirOf("ShipS")){
			
			Sleep(rand(200)+100); // prevent cycling advance->engage->advance->...
			//Sleep(1000);
            .AddCommand(true, "advance", pt);
			.AddCommand(true, "engage");
			return;
		}
	}
 
	//pr("advance: no target in sight - returning to source point!");
	if(.Goto(pt, 0, 1500, true, 0)) return;
	
}
